(:
  give a level according to how many posts $userfacet posted
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display>{
let $t := $arguments[1]
let $userfacet := $arguments[2]
let $d := doc($t)/display/item
let $sortedusers := for $user in fn:distinct-values(for $e in $d return xs:string($e/info[@name=$userfacet]))
           let $count := fn:count($d[info[@name=$userfacet]=$user])
order by $count
return $user

for $e at $p in $d
let $level := fn:index-of($sortedusers,xs:string($e/info[@name=$userfacet]))
return
  <item>{
    <info>{ 
    	attribute name {"level-for-user"},
    	xs:string($level)
    }</info>,
    (  let $inf := $e/info
       for $i in $inf
       return
         if ($i[name="level-for-user"]) then
             ()
         else
             $i
    )
  }</item>

}</display>
